﻿-- CREATE TRIGGER trgchange_quotaion_info
--   AFTER INSERT OR UPDATE or DELETE
--   ON sale_order
--   FOR EACH ROW
--   EXECUTE PROCEDURE trgfn_delete_update_delete_quotation_update_project_contract();

-- CREATE TRIGGER trgupdate_tax_when_null
-- 	before INSERT OR UPDATE
-- 	ON sale_order
-- 	FOR EACH ROW
-- 	EXECUTE PROCEDURE trgfn_insert_update_quotation_update_tax_percent();

CREATE OR REPLACE FUNCTION trgfn_insert_update_quotation_update_tax_percent()
                        RETURNS trigger AS
                        $BODY$
                        Begin
				if new.taxes_percent is null then
					new.taxes_percent=0;
				end if;
				return new;
			end;    
                        $BODY$
                      LANGUAGE plpgsql VOLATILE;



CREATE OR REPLACE FUNCTION trgfn_delete_update_delete_quotation_update_project_contract()
                        RETURNS trigger AS
                        $BODY$
                        DECLARE
				using_id integer;
				contract_chk boolean:=False;
				project_e_chk boolean:=False;
				project_m_chk boolean:=False;
                        Begin
                        -- ### Begin Update Trigger ###
                            If TG_OP='UPDATE' then
                            --Begin Change Amount in Quotation--
                                if coalesce(new.approved_amount,0)<>coalesce(old.approved_amount,0) then
                                    if coalesce(new.approved_amount,0)=0 then
                                        update sale_order set negotiating='Negotiated' where id=new.id;
                                    else
                                        update sale_order set negotiating='Fixed' where id=new.id;
                                    end if;        
                                end if;
                               
				if new.amount<>old.amount or new.amount_tax <> old.amount_tax then
					update sale_order set amount_total=new.amount+new.amount_tax where id=new.id;
				end if;
				
                                if new.q_amount_e<>old.q_amount_e or new.q_amount_e_other <> old.q_amount_e_other or new.q_amount_e_discount<>old.q_amount_e_discount then
					update sale_order set q_amount_total_e=new.q_amount_e+new.q_amount_e_other+q_amount_e_discount where id=new.id;
					update sale_order set approved_amount=q_amount_total_e+q_amount_total_m,
							      amount_tax_approved=(q_amount_total_e+q_amount_total_m)*coalesce(new.taxes_percent,0)/100.0,
							      amount_total_approved=(q_amount_total_e+q_amount_total_m)*(1+coalesce(new.taxes_percent,0)/100.0)
							      where id=new.id;
				end if;
                                if new.q_amount_m<>old.q_amount_m or new.q_amount_m_other <> old.q_amount_m_other or new.q_amount_m_discount<>old.q_amount_m_discount then
					update sale_order set q_amount_total_m=new.q_amount_m+new.q_amount_m_other+q_amount_m_discount where id=new.id;
					update sale_order set approved_amount=q_amount_total_e+q_amount_total_m,
							      amount_tax_approved=(q_amount_total_e+q_amount_total_m)*coalesce(new.taxes_percent,0)/100.0,
							      amount_total_approved=(q_amount_total_e+q_amount_total_m)*(1+coalesce(new.taxes_percent,0)/100.0)
							      where id=new.id;
				end if;
		--END--				
--########## Mechanical ##########
		--If change project M--
				--Update Project Amount, VAT, Total M when change Approved Amount M - New 
				if 
					(new.project_m_id<>old.project_m_id) or
					(new.project_m_id is null and old.project_m_id>0) or
					(new.project_m_id>0 and old.project_m_id is null) or
					(new.project_m_id is null and old.project_m_id is null)
				then 
					using_id=new.project_m_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(m_break_total_in_usd,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(m_break_total_in_usd,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(m_break_total_in_usd,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;

					-- ## Update for Old Project ##					
					using_id=old.project_m_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(m_break_total_in_usd,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(m_break_total_in_usd,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(m_break_total_in_usd,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
				end if;
				if new.q_amount_total_m<>old.q_amount_total_m or new.state<>old.state then 
					using_id=new.contract_id;
					
					update 
						kdvn_contract_client 
					Set 
						contract_amount_e=(Select round(coalesce(sum(q_amount_total_e),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_amount_m=(Select round(coalesce(sum(q_amount_total_m),0),coalesce(accuracy,2))  from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_vat=(Select round(coalesce(sum(amount_tax_approved::numeric),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_total=(Select round(sum(coalesce(amount_total_approved,0)),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy)
					where id=using_id ;
					--Moi Them

					using_id=new.project_m_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
				end if;
				
--########## Electrical ##########
				if
					(new.project_e_id<>old.project_e_id) or
					(new.project_e_id is null and old.project_e_id>0) or
					(new.project_e_id>0 and old.project_e_id is null) or
					(new.project_e_id is null and old.project_e_id is null)
				then
				--if new.project_e_id<>old.project_e_id then 
					-- ## Update for New Project ##
					using_id=new.project_e_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else e_break_total_in_usd/cal_exrate end,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(e_break_total_in_usd,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(e_break_total_in_usd,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;

					-- ## Update for Old Project ##					
					using_id=old.project_e_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(e_break_total_in_usd,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(e_break_total_in_usd,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(e_break_total_in_usd,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
				end if;
--Update Project Amount, VAT, Total E when change Approved Amount E
				if new.q_amount_total_e<>old.q_amount_total_e or new.state<>old.state then 
--Duong Them
					using_id=new.contract_id;
					
					update 
						kdvn_contract_client 
					Set 
						contract_amount_e=(Select round(coalesce(sum(q_amount_total_e),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_amount_m=(Select round(coalesce(sum(q_amount_total_m),0),coalesce(accuracy,2))  from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_vat=(Select round(coalesce(sum(amount_tax_approved::numeric),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_total=(Select round(sum(coalesce(amount_total_approved,0)),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy)
					where id=using_id ;

					using_id=new.project_e_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
				end if;
--########## End of Electrical ##########
				if
					(new.contract_id<>old.contract_id) or
					(new.contract_id is null and old.contract_id>0) or
					(new.contract_id>0 and old.contract_id is null)
				then
					-- New ID--
					using_id = new.contract_id;
					update 
						kdvn_contract_client 
					Set 
						contract_amount_e=(Select round(coalesce(sum(q_amount_total_e),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_amount_m=(Select round(coalesce(sum(q_amount_total_m),0),coalesce(accuracy,2))  from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_vat=(Select round(coalesce(sum(amount_tax_approved::numeric),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_total=(Select round(sum(coalesce(amount_total_approved,0)),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy)
					where id=using_id ;
					
					-- OLD ID--
					using_id = old.contract_id;
					update 
						kdvn_contract_client 
					Set 
						contract_amount_e=(Select round(coalesce(sum(q_amount_total_e),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_amount_m=(Select round(coalesce(sum(q_amount_total_m),0),coalesce(accuracy,2))  from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_vat=(Select round(coalesce(sum(amount_tax_approved::numeric),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_total=(Select round(sum(coalesce(amount_total_approved,0)),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy)
					where id=using_id ;
				end if;
				if 
					old.approved_amount<>new.approved_amount or
					old.amount_tax_approved<>new.amount_tax_approved or
					old.amount_total_approved<>new.amount_total_approved 
				then
					using_id = new.contract_id;
					update 
						kdvn_contract_client 
					Set 
						contract_amount_e=(Select round(coalesce(sum(q_amount_total_e),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_amount_m=(Select round(coalesce(sum(q_amount_total_m),0),coalesce(accuracy,2))  from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_vat=(Select round(coalesce(sum(amount_tax_approved::numeric),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
						contract_total=(Select round(sum(coalesce(amount_total_approved,0)),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy)
					where id=using_id ;
				end if;


--Check if change amount USD to update to project
				if new.cal_exrate<>old.cal_exrate then 
					using_id=new.project_m_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_m_id=using_id
										group by 
											project_m_id)
					where id=using_id;

--Electrical
					using_id=new.project_e_id;
					Update kdvn_project set project_amount=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0)) as project_amount
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_vat=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0))*max(coalesce(taxes_percent,0))/100 as project_vat
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
					Update kdvn_project set project_total=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0))*(max(coalesce(taxes_percent,0))/100+1)
										from 
											sale_order
										where 
											state='done' and project_e_id=using_id
										group by 
											project_e_id)
					where id=using_id;
				end if;
				
				return new;
-- ### End Of Update Trigger ###
                            
-- ### Begin Of Insert Trigger ###
                            ELSEIF TG_OP='INSERT' then
				if coalesce(new.approved_amount,0)>0 then
					update sale_order set negotiating='Fixed' where id=new.id;
				else
					update sale_order set negotiating='Negotiated' where id=new.id;
                                end if;
                                -- Project Mechanical --
				if new.state='done' then
					if new.project_m_id>0 then --Update Project Amount, VAT, Total M WHEN change Project M ID
						using_id=new.project_m_id;
						Update kdvn_project set project_amount=(Select
											sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0)) as project_amount
											from 
												sale_order
											where 
												state='done' and project_m_id=using_id
											group by 
												project_m_id)
						where id=using_id;
						Update kdvn_project set project_vat=(Select
												sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0))*max(coalesce(taxes_percent,0))/100 as project_vat
											from 
												sale_order
											where 
												state='done' and project_m_id=using_id
											group by 
												project_m_id)
						where id=using_id;
						Update kdvn_project set project_total=(Select
												sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_m/cal_exrate end,0))*(max(coalesce(taxes_percent,0))/100+1)
											from 
												sale_order
											where 
												state='done' and project_m_id=using_id
											group by 
												project_m_id)
						where id=using_id;
					end if;
				
					--Electrical
					if new.project_e_id>0 then --Update Project Amount, VAT, Total E WHEN change Project E ID
						using_id=new.project_e_id;
						Update kdvn_project set project_amount=(Select
												sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0)) as project_amount
											from 
												sale_order
											where 
												state='done' and project_e_id=using_id
											group by 
												project_e_id)
						where id=using_id;
						Update kdvn_project set project_vat=(Select
												sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0))*max(coalesce(taxes_percent,0))/100 as project_vat
											from 
												sale_order
											where 
												state='done' and project_e_id=using_id
											group by 
												project_e_id)
						where id=using_id;
						Update kdvn_project set project_total=(Select
												sum(coalesce(case when coalesce(cal_exrate,0)=0 then 0 else q_amount_total_e/cal_exrate end,0))*(max(coalesce(taxes_percent,0))/100+1)
											from 
												sale_order
											where 
												state='done' and project_e_id=using_id
											group by 
												project_e_id)
						where id=using_id;
					end if;
					if new.contract_id>0 then
					--# Contract
						using_id = new.contract_id;
						update 
							kdvn_contract_client 
						Set 
							contract_amount_e=(Select round(coalesce(sum(q_amount_total_e),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
							contract_amount_m=(Select round(coalesce(sum(q_amount_total_m),0),coalesce(accuracy,2))  from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
							contract_vat=(Select round(coalesce(sum(amount_tax_approved::numeric),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
							contract_total=(Select round(sum(coalesce(amount_total_approved,0)),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy)
						where id=using_id ;						
					end if;
					
				end if;
				return new;
			--### End Of Insert Trigger ###
			    ELSEIF TG_OP='DELETE' then
                                --Mechanical
                                if old.state='done' then
					if old.project_m_id>0 then --Update Project Amount, VAT, Total M WHEN change Project M ID
						using_id=old.project_m_id;
						Update kdvn_project set project_amount=(Select
												sum(coalesce(m_break_total_in_usd,0)) as project_amount
											from 
												sale_order
											where 
												state='done' and project_m_id=using_id
											group by 
												project_m_id)
						where id=using_id;
						Update kdvn_project set project_vat=(Select
												sum(coalesce(m_break_total_in_usd,0))*max(coalesce(taxes_percent,0))/100 as project_vat
											from 
												sale_order
											where 
												state='done' and project_m_id=using_id
											group by 
												project_m_id)
						where id=using_id;
						Update kdvn_project set project_total=(Select
												sum(coalesce(m_break_total_in_usd,0))*(max(coalesce(taxes_percent,0))/100+1)
											from 
												sale_order
											where 
												state='done' and project_m_id=using_id
											group by 
												project_m_id)
						where id=using_id;
					end if;
				--Electrical
					if old.project_e_id>0 then --Update Project Amount, VAT, Total E WHEN change Project E ID
						using_id=old.project_e_id;
						Update kdvn_project set project_amount=(Select
												sum(coalesce(e_break_total_in_usd,0)) as project_amount
											from 
												sale_order
											where 
												state='done' and project_e_id=using_id
											group by 
												project_e_id)
						where id=using_id;
						Update kdvn_project set project_vat=(Select
												sum(coalesce(e_break_total_in_usd,0))*max(coalesce(taxes_percent,0))/100 as project_vat
											from 
												sale_order
											where 
												state='done' and project_e_id=using_id
											group by 
												project_e_id)
						where id=using_id;
						Update kdvn_project set project_total=(Select
												sum(coalesce(e_break_total_in_usd,0))*(max(coalesce(taxes_percent,0))/100+1)
											from 
												sale_order
											where 
												state='done' and project_e_id=using_id
											group by 
												project_e_id)
						where id=using_id;
					end if;
					--# Contract
					if old.contract_id>0 then
						using_id = old.contract_id;
						update 
							kdvn_contract_client 
						Set 
							contract_amount_e=(Select round(coalesce(sum(q_amount_total_e),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
							contract_amount_m=(Select round(coalesce(sum(q_amount_total_m),0),coalesce(accuracy,2))  from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
							contract_vat=(Select round(coalesce(sum(amount_tax_approved::numeric),0),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy),
							contract_total=(Select round(sum(coalesce(amount_total_approved,0)),coalesce(accuracy,2)) from sale_order so left join res_currency rc on so.currency_id = rc.id where state='done' and so.contract_id=using_id  group by accuracy)
						where id=using_id ;						
					end if;
				end if;
				return old;
			end if; --End if TG_OP
                        end;    
                        $BODY$
                      LANGUAGE plpgsql VOLATILE
                      COST 100;